<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="search">
        <el-form-item>
          <el-input v-model="search.key" placeholder="菜单名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="url" label="路径" width="120" sortable>
      </el-table-column>
      <el-table-column prop="icon" label="icon" width="120" sortable>
      </el-table-column>
      <el-table-column prop="children.name" label="上级菜单" width="120" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template scope="scope">
          <el-button size="small" plain @click="handleSave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" plain size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!--
           el-pagination 表示分页
           @size-change : 每页条数改变的时候会触发的方法
           @current-change: 当页数改变的时候
           :current-page: 绑定当前页数据
           :page-sizes="[100, 200, 300, 400]" : 每页多少条的选择
           :page-size="100" 每页的条数
           :total="400" 总条数
           layout="total, sizes, prev, pager, next, jumper"
      -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="search.currentPage"
          :page-sizes="[10,15,20]"
          :page-size="100"
          style="float: right"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
    </el-col>
    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px"  ref="addForm">
        <el-form-item label="菜单名称" prop="name" >
          <el-input v-model="saveForm.name" auto-complete="off" id="name"></el-input>
        </el-form-item>
        <el-form-item label="部门路径" prop="url" >
          <el-input v-model="saveForm.url" auto-complete="off" id="url"></el-input>
        </el-form-item>

        <!--        <el-form-item label="上级菜单" prop="parent">-->
        <!--          <el-select v-model="saveForm.parent" clearable value-key="id" placeholder="请选择" id="parent">-->
        <!--            <el-option-->
        <!--                v-for="item in menuTree"-->
        <!--                :key="item.id"-->
        <!--                :label="item.name"-->
        <!--                :value="item">-->
        <!--              <span style="float: left">{{ item.name }}</span>-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

export default {
  data() {
    return {
      menuTree:[],
      search: {
        key: '',
        pageSize:5,
        currentPage:1
      },
      pageInfo:{
        data: [],
        total: 0,
      },
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible:false,
      saveForm:{
        id:null,
        name:"",
        url:"",
        icon:"",
        children:{
          id:null,
          name:"",
        },
        parent:{
          id:null,
          name:"",
        },
      }
    }
  },
  methods: {
    // 查菜单树
    getMenuTree(){
      this.$http.get("/menu/menuTree")
          .then(res=>{
            res=res.data;
            console.log(res.data)
            if(res.success){
              this.menuTree = res.data;
            }
          })
          .catch(res=>{})
    },
    // 点击查询的方法
    queryData(){
      this.search.currentPage = 1
      this.getMenus()
    },
    // 每页条数变动的时候
    handleSizeChange(val) {
      this.search.pageSize = val
      this.getMenus()
    },

    handleCurrentChange(val) {
      // val 指的是当前的页数
      this.search.currentPage = val;
      this.getMenus();
    },
    //获取菜单列表
    getMenus() {
      this.listLoading=true;
      this.$http.post("/menu",this.search)
          .then(res=>{
            this.listLoading=false;
            res = res.data;
            console.log(res)
            if(res.success){
              this.pageInfo = res.data
            }
          })
          .catch(res=>{})
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/menu/"+row.id)
            .then(res=>{
              res = res.data
              this.listLoading = false;
              if(res.success){
                this.$message({type:"success",message:"删除成功"});
              }else{
                this.$message.error("删除失败"+res.msg)
              }
              this.search.currentPage= 1;
              this.getMenus();
            })
            .catch(res=>{})
      }).catch(() => {

      });
    },
    //显示保存界面
    handleSave: function (index,row) {
      this.getMenuTree();
      this.saveFormVisible = true;
      if (row){
        this.saveForm = JSON.parse(JSON.stringify(row));
        // console.log("this.saveForm",this.saveForm)
        if(!row.parent){
          this.saveForm.parent={
            id:null,
            name:""
          }
        }
      }else {
        this.saveForm= {
          id:null,
          name:"",
          intro:"",
          manager:{
          },
          parent:{
          },
          state:""
        }
      }
    },

    //新增
    saveSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {

            let para = Object.assign({}, this.saveForm);
            console.log(para)
            var parent = para.parent;

            if (parent.length >= 0){
              if (parent.length==0){
                para.parent={id:null,name:""}
              }else{
                para.parent={id:parent[parent.length-1],name:""}
              }
            }
            console.log(para)
            // 发送请求
            this.$http.put("/menu",para)
                .then(res=>{
                  this.saveFormVisible= false
                  this.$message({type:"success",message:"保存成功"});
                  this.getMenus();
                })
                .catch(res=>{})
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送请求
        this.$http.patch("/menu",ids)
            .then(res=>{
              res = res.data
              this.listLoading = false;
              if(res.success){
                this.$message({type:"success",message:"删除成功"});
              }else{
                this.$message.error("删除失败"+res.msg)
              }
              this.search.currentPage= 1;
              this.getMenus();
            })
            .catch(res=>{})
      }).catch(() => {});
    }
  },
  mounted() {
    this.getMenus();
  }
}
</script>

<style scoped>

</style>